package com.link.base.core.basic.util.datax;

import java.util.ArrayList;
import java.util.List;

/**
 * 酒馆购小程序
 * @author 何启谦
 * @date 2019/4/26
 */
public class JiuGuanGou {

    /**
     * 产品
     */
    public static String prodTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "id";
        //临时表名称
        String tempTableName = "jgg_pub_goods";
        //正式表名称
        String targetTableName = "lnk_prod";
        //填充字段信息
        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("id", "prod_code", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("goods_title", "prod_name", "VARCHAR(200)"));
        fieldList.add(new FieldMapping("goods_price", "price", "DECIMAL(22,7)"));
        fieldList.add(new FieldMapping("goods_spec", "prod_standard", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("remark", "comments", "VARCHAR(1000)"));
        fieldList.add(new FieldMapping("(case when t1.delete_mark = '0' then 'N' else 'Y' end)", "is_effective", "CHAR(1)"));
        fieldList.add(new FieldMapping("'wxc7d9afe6fd99b728'", "prod_good_type", "VARCHAR(50)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName,
                fieldList, "prod_good_type");
    }

    /**
     * 订单头
     */
    public static String orderTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "id";
        //临时表名称
        String tempTableName = "jgg_pub_order";
        //正式表名称
        String targetTableName = "lnk_sale_order";
        //填充字段信息
        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("id", "order_no", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("create_time", "order_date", "DATETIME"));
        fieldList.add(new FieldMapping("subtotal", "ord_amount", "DECIMAL(22,7)"));
        fieldList.add(new FieldMapping("price", "order_total_amount", "DECIMAL(22,7)"));
        fieldList.add(new FieldMapping("use_time", "finish_time", "DATETIME"));
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_accnt s1 "
                + "where s1.ACCT_CHANNEL = 'wxc7d9afe6fd99b728' and s1.integration_id = t1.user_id limit 1)",
                "acct_id", "varchar(50)").setRequired(true));
        fieldList.add(new FieldMapping("'wxc7d9afe6fd99b728'", "source_from", "VARCHAR(50)"));
        fieldList.add(new FieldMapping("'SalesOrder'", "order_type", "VARCHAR(100)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName,
                fieldList, "source_from");
    }

    /**
     * 订单行
     */
    public static String orderItemTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "id";
        //临时表名称
        String tempTableName = "jgg_pub_order_goods";
        //正式表名称
        String targetTableName = "lnk_sale_order_item";
        //填充字段信息
        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_sale_order s1 where "
                + " s1.source_from = 'wxc7d9afe6fd99b728' and s1.integration_id = t1.order_id limit 1)",
                "head_id", "varchar(50)").setRequired(true));
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_prod s1 "
                + "where s1.prod_good_type = 'wxc7d9afe6fd99b728' and s1.integration_id = t1.goods_id limit 1)",
                "prod_id", "varchar(50)").setRequired(true));
        fieldList.add(new FieldMapping("goods_number", "prod_num", "DECIMAL(22,7)"));
        fieldList.add(new FieldMapping("(select s1.price from linkcrm.lnk_prod s1 where "
                + "s1.prod_good_type = 'wxc7d9afe6fd99b728' and s1.integration_id = t1.goods_id limit 1)", "base_pri", "DECIMAL(22,7)"));
        fieldList.add(new FieldMapping("'wxc7d9afe6fd99b728'", "order_source", "varchar(50)"));
        // fieldList.add(new FieldMapping("'Common Item'", "item_type", "VARCHAR(50)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName,
                fieldList, "order_source");
    }

    /**
     * 用户（客户）
     */
    public static String accntTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "id";
        //临时表名称
        String tempTableName = "jgg_weixin_user_user";
        //正式表名称
        String targetTableName = "lnk_accnt";
        //填充字段信息
        List<FieldMapping> fieldList = new ArrayList<>();

        fieldList.add(new FieldMapping("lc_last_sync_time", "syn_date", "DATETIME"));
        fieldList.add(new FieldMapping("create_time", "qd_sync_date", "DATETIME"));
        fieldList.add(new FieldMapping("create_time", "wx_subscribe_time", "DATETIME"));
        fieldList.add(new FieldMapping("source", "ACCT_CHANNEL", "VARCHAR(50)"));
        fieldList.add(new FieldMapping("phone", "mobile_phone", "varchar(50)"));
        fieldList.add(new FieldMapping("avatar", "wx_headimgurl", "VARCHAR(200)"));
        fieldList.add(new FieldMapping("open_id", "wx_open_id", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("source", "app_id", "VARCHAR(50)"));
        fieldList.add(new FieldMapping("`sync_temp`.lnk_urldecode(t1.nick_name)", "wx_nickname", "VARCHAR(200)"));
        fieldList.add(new FieldMapping("(case t1.sex when '0' then 'UNKNOWN' when '1' then 'MALE' when '2' then 'FEMALE' else t1.sex end)",
                "gender", "varchar(50)"));
        fieldList.add(new FieldMapping("city", "city", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("country", "country", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("province", "province", "VARCHAR(100)"));
        fieldList.add(new FieldMapping("user_language", "wx_language", "VARCHAR(10)"));
        fieldList.add(new FieldMapping("union_id", "wx_unionid", "varchar(50)"));
        fieldList.add(new FieldMapping("'C'", "acct_level", "VARCHAR(50)"));
        fieldList.add(new FieldMapping("'MiniProgramsConsumer'", "acct_type", "VARCHAR(50)"));

        fieldList.add(new FieldMapping("referrer", "subscribe_scene", "varchar(50)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName,
                fieldList, "ACCT_CHANNEL");
    }
}
